package com.ruoyi.framework.config;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.ruoyi.common.utils.SecurityUtils;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MybatisPlusHandler implements MetaObjectHandler {

    static String getUsername(){
        return SecurityUtils.getUsername();
    }

    // 插入时的填充策略
    @Override
    public void insertFill(MetaObject metaObject) {

        this.setFieldValByName("createBy", MybatisPlusHandler.getUsername(),metaObject);
        this.setFieldValByName("createTime", new Date(),metaObject);
    }
    // 更新时的填充策略
    @Override
    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateBy",MybatisPlusHandler.getUsername(),metaObject);
        this.setFieldValByName("updateTime",new Date(),metaObject);
    }
}
